from rest_framework import serializers

from shops.models import ShopsModel, BannerModel, ShopCategoryModel, ChildCategoryModel, ShopActivityModel


class ShopActivitySerializer(serializers.ModelSerializer):

    class Meta:
        model = ShopActivityModel
        fields = '__all__'


class ChildCategoryModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = ChildCategoryModel
        fields = '__all__'


class ShopsSerializer(serializers.ModelSerializer):

    def to_representation(self, instance):
        data = super().to_representation(instance)
        active = instance.shopactivitymodel_set.filter(shop_id=instance.shop_id).first()
        if active:
            target = active.target
            cut = active.cut
            data['active'] = {
                'target': target,
                'cut': cut
            }
        return data

    class Meta:
        model = ShopsModel
        fields = '__all__'


class BannerViewSerializer(serializers.ModelSerializer):

    class Meta:
        model = BannerModel
        fields = ['banner_id', 'pic_url', 'pic_name']


class ShopCategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = ShopCategoryModel
        fields = '__all__'


class ChildCategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = ChildCategoryModel
        fields = '__all__'


class ShopsRecommendSerializer(serializers.ModelSerializer):

    class Meta:
        model = ChildCategoryModel
        fields = '__all__'
